﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;

namespace CMS.Web.CompDenu
{
    public partial class AddXLDonThuGrid : BaseUserControl, IDonThuXLListView
    {
        DonThuXLListPresenter presenter;
        IList<ChuyenTiep> objIList;
        bool isRole = false;
        bool isEdit = true;
        NguoiSuDungInfo userInfo;
        public string docID = "";// mã đơn thư

        public AddXLDonThuGrid()
        {
            this.presenter = new DonThuXLListPresenter(this, NHibernateDaoFactory.Instance.GetChuyenTiepDao());
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter.CancelUpdateEvent += new EventHandler(HandleCancelUpdateEvent);
        }

        private void HandleCancelUpdateEvent(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            docID = Request.QueryString.Get("objID");
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            presenter.LoadViewFromModel(docID, userInfo.ID);
            if (!this.IsPostBack)
            {
                this.gridObj.DataSource = this.objIList;
                this.gridObj.DataBind();
            }
            this.lbnXoa.Attributes.Add("onclick", "return doSetStatus(2)");
        }               

        #region Grid Members
        // For swich page
        protected void gridObj_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridObj.DataSource = this.objIList;
            gridObj.PageIndex = e.NewPageIndex;
            gridObj.DataBind();

        }
        #endregion

        #region IDonThuView Members

        public bool IsValid
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public IList<ChuyenTiep> ChuyenTiepList
        {
            get { return this.objIList; }
            set {
                this.objIList = value;
                //foreach (DonThu item in value)
                //{
                //    //if (item.MaTTXL == 3)
                //    //{
                //    //    SoDTDaXL++;
                //    //}
                //    //else
                //    //{
                //    //    SoDTChuaXL++;
                //    //}
                //}
            }

        }
        #endregion

        #region Button members
        // For Delete button
        protected void lbnXoa_Click(object sender, EventArgs e)
        {
            string code = "";
            foreach (GridViewRow row in gridObj.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                if (cb.Checked)
                {
                    //code = "'" + Convert.ToString(gridObj.DataKeys[row.RowIndex].Value) + "',";
                    code = Convert.ToString(gridObj.DataKeys[row.RowIndex].Value);
                    presenter.ObjToDelete(code);

                }
            }
            presenter.LoadViewFromModel(docID,userInfo.ID);
            this.gridObj.DataSource = this.ChuyenTiepList;
            gridObj.DataBind();
        }

        // For Add buuton
        protected void lbnThem_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&tab=2&type=pro&action=EDIT&objID="+docID);
        }

        // For Update Status button
        protected void lbnHienThi_Click(object sender, EventArgs e)
        {
            //foreach (GridViewRow row in gridObj.Rows)
            //{
            //    CheckBox cb = (CheckBox)row.FindControl("chkSelect");

            //    if (cb.Checked)
            //    {
            //        string code = Convert.ToString(gridObj.DataKeys[row.RowIndex].Value);
            //        presenter.ObjToUpdate(code);

            //    }
            //}
            //presenter.LoadViewFromModel(userInfo.ID);
            //this.gridObj.DataSource = this.DonThuList;
            //gridObj.DataBind();
        }
        #endregion
                

        #region IPhongBanView Members


        public bool IsRole
        {
            get
            {
                return this.isRole;
            }
            set
            {
                this.isRole = value;
                this.lbnXoa.Visible = value;
            }
        }

        public bool IsEdit
        {
            get
            {
                return this.isEdit;
            }
            set
            {
                this.isEdit = value;
            }
        }
        #endregion               
        
    }
}